package com.webserver.controller;

import com.webserver.annotation.Controller;
import com.webserver.annotation.RequestMapping;
import com.webserver.http.HttpServletRequest;
import com.webserver.http.HttpServletResponse;
import qrcode.QRCodeUtil;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;

/**
 * tool 工具
 * 工具
 */
@Controller
public class ToolsController {
    @RequestMapping("/myweb/random.jpg")
    public void randomCode(HttpServletRequest request, HttpServletResponse response){
        System.out.println("开始生成验证码图片...");
        //创建一个画布
        BufferedImage image = new BufferedImage(70,30,BufferedImage.TYPE_INT_RGB);
        //通过画布获取一根笔，通过这根笔就可以往该画布上画画了
        Graphics g = image.getGraphics();
        Random random = new Random();//用来生成随机数
        //画背景
        //1给画笔蘸颜色
        Color bgcolor = new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256));
        g.setColor(bgcolor);
        //2图满整个画图
        g.fillRect(0,0,70,30);

        String line = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        for(int i=0;i<4;i++) {
            //在画图上画一个字符
            //1给画笔换个颜色
            Color color = new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256));
            g.setColor(color);
            //2给画笔设置字体
            g.setFont(new Font(null, Font.BOLD, 20));
            String s = line.charAt(random.nextInt(line.length())) + "";
            g.drawString(s, i*15+5, 18+random.nextInt(11)-5);
        }

        //画干扰线
        for(int i=0;i<5;i++) {
            Color color = new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256));
            g.setColor(color);
            g.drawLine(random.nextInt(71), random.nextInt(31), random.nextInt(71), random.nextInt(31));
        }


        try {
            ImageIO.write(image,"jpg",response.getOutputStream());
            response.setContentType("image/jpeg");
            System.out.println("图片生成完毕");
        } catch (IOException e) {
            e.printStackTrace();
        }

        System.out.println("验证码图片生成完毕!");
    }
    @RequestMapping("/myweb/createQR")
    public void createQR(HttpServletRequest request, HttpServletResponse response){
        String line = request.getParameter("content");

        OutputStream out = response.getOutputStream();
        try {
            QRCodeUtil.encode(line,"./logo.jpg",out,true);
            response.setContentType("image/jpeg");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

//    public static void main(String[] args) {
//        String line = "beep beep i'am sheep";
//
//        try {
//            /*
//                encode(String content,String fileName)
//                参数1:二维码上显示的文字
//                参数2:生成的图片路径
//            */
////            QRCodeUtil.encode(line,"./qr.jpg");
//            /*
//                encode(String content,String imagePath,String fileName,boolean needC)
//                参数1:二维码上显示的文字
//                参数2:二维码中间的logo图片的路径
//                参数3:生成的图片路径
//                参数4:logo图片是否需要压缩。如果logo图比较大会盖住生成的二维码，因此通常需要压缩
//             */
//            QRCodeUtil.encode(line,"./logo.jpg","./qr.jpg",true);
//
//            /*
//                encode(String content,String imagePath,OutputStream out,boolean needC)
//                参数1:二维码上显示的文字
//                参数2:二维码中间的logo图片的路径
//                参数3:生成的图片数据会通过给定的流写出
//                参数4:logo图片是否需要压缩。如果logo图比较大会盖住生成的二维码，因此通常需要压缩
//             */
//            FileOutputStream fos = new FileOutputStream("./qr.jpg");
//            QRCodeUtil.encode(line,"./logo.jpg",fos,true);
//            System.out.println("二维码生成完毕!");
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }

    public static void main(String[] args) {
        //创建一个画布
        BufferedImage image = new BufferedImage(70,30,BufferedImage.TYPE_INT_RGB);
        //通过画布获取一根笔，通过这根笔就可以往该画布上画画了
        Graphics g = image.getGraphics();

        Random random = new Random();//用来生成随机数

        //画背景
        //1给画笔蘸颜色
        Color bgcolor = new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256));
        g.setColor(bgcolor);
        //2图满整个画图
        g.fillRect(0,0,70,30);

        String line = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        for(int i=0;i<4;i++) {
            //在画图上画一个字符
            //1给画笔换个颜色
            Color color = new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256));
            g.setColor(color);
            //2给画笔设置字体
            g.setFont(new Font(null, Font.BOLD, 20));
            String s = line.charAt(random.nextInt(line.length())) + "";
            g.drawString(s, i*15+5, 18+random.nextInt(11)-5);
        }

        //画干扰线
        for(int i=0;i<5;i++) {
            Color color = new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256));
            g.setColor(color);
            g.drawLine(random.nextInt(71), random.nextInt(31), random.nextInt(71), random.nextInt(31));
        }


        try {
            ImageIO.write(image,"jpg",new FileOutputStream("./random.jpg"));
            System.out.println("图片生成完毕");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
